//package com.waibao.im.service.impl;
//
//import com.lianqu.huanxin.core.model.Authentic;
//import com.lianqu.huanxin.core.model.TalkMsg;
//import com.lianqu.huanxin.core.model.HXResponseObject;
//import com.lianqu.huanxin.core.service.TalkDataService;
//import com.lianqu.huanxin.core.service.TalkHttpService;
//import com.lianqu.huanxin.core.tool._Global;
//
//import java.io.File;
//import java.net.URLEncoder;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class TalkDataServiceImpl implements TalkDataService {
//	private TalkHttpService service;
//	private Authentic auth;
//
//	// TODO @Autowired
//	public TalkDataServiceImpl(TalkHttpService service) {
//		this.service = service;
//		this.auth = new Authentic(service);
//	}
//
//	public void setToken(Authentic.Token token) {
//		this.auth = new Authentic(service, token);
//	}
//
//	public HXResponseObject login(String username, String password) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("grant_type", "password");
//		param.put("username", username);
//		param.put("password", password);
//		return service.request(_Global.URR_TOKEN, _Global.HTTP_METHOD_POST,
//				param, null, null);
//	}
//
//	public HXResponseObject logout(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/disconnect", _Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND);
//	}
//
//	public HXResponseObject userSave(String username, String password, String nickname)
//			throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("username", username);
//		param.put("password", password);
//		param.put("nickname", nickname);
//		return service.request(_Global.URL_USER, _Global.HTTP_METHOD_POST,
//				param, auth, null);
//	}
//
//	public HXResponseObject userSave(String[] username, String[] password,
//			String[] nickname) throws Exception {
//		if (username == null || password == null
//				|| username.length != password.length
//				|| (nickname != null && username.length != nickname.length)) {
//			return null;
//		}
//		if (nickname == null) {
//			nickname = new String[username.length];
//		}
//		List<Map<String, Object>> param = new ArrayList<Map<String, Object>>();
//		for (int i = 0; i < username.length; i++) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("username", username[i]);
//			map.put("password", password[i]);
//			map.put("nickname", nickname[i]);
//			param.add(map);
//		}
//		return service.request(_Global.URL_USER, _Global.HTTP_METHOD_POST,
//				param, auth, null);
//	}
//
//	public HXResponseObject userDrop(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username,
//				_Global.HTTP_METHOD_DELETE, null, auth, null);
//	}
//
//	public HXResponseObject userDrop(Long size) throws Exception {
//		return service.request(_Global.URL_USER + "?limit="
//				+ (size != null ? size : _Global.APP_PAGE_SIZE),
//				_Global.HTTP_METHOD_DELETE, null, auth, null);
//	}
//
//	public HXResponseObject userModifyAccess(String username, Boolean access)
//			throws Exception {
//		return service.request(_Global.URL_USER + "/" + username + "/"
//				+ (access != null && access ? "activate" : "deactivate"),
//				_Global.HTTP_METHOD_POST, null, auth, null);
//	}
//
//	public HXResponseObject userModifyPassword(String username, String password)
//			throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("newpassword", password);
//		return service.request(_Global.URL_USER + "/" + username + "/password",
//				_Global.HTTP_METHOD_PUT, param, auth, null);
//	}
//
//	public HXResponseObject userModifyNickname(String username, String nickname)
//			throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("nickname", nickname);
//		return service.request(_Global.URL_USER + "/" + username,
//				_Global.HTTP_METHOD_PUT, param, auth, null);
//	}
//
//	public HXResponseObject userGet(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username,
//				_Global.HTTP_METHOD_GET, null, auth, null);
//	}
//
//	public HXResponseObject userList(Long size, String cursor, Long start)
//			throws Exception {
//		return service.request(
//				_Global.URL_USER + "?" + this.page(size, cursor, start),
//				_Global.HTTP_METHOD_GET, null, auth, null);
//	}
//
//	public HXResponseObject userLine(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username + "/status",
//				_Global.HTTP_METHOD_GET, null, auth, HXResponseObject.DATA_MAP);
//	}
//
//	public HXResponseObject userGroupList(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/joined_chatgroups", _Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_GROUP_LIST);
//	}
//
//	public HXResponseObject userRoomList(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/joined_chatrooms", _Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_ROOM_LIST);
//	}
//
//	public HXResponseObject userMessageCount(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/offline_msg_count", _Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_MAP);
//	}
//
//	public HXResponseObject userMessageLine(String username, String id)
//			throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/offline_msg_status/" + id, _Global.HTTP_METHOD_GET, null,
//				auth, HXResponseObject.DATA_MAP);
//	}
//
//	public HXResponseObject friendSave(String username, String friend) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/contacts/users/" + friend, _Global.HTTP_METHOD_POST, null,
//				auth, null);
//	}
//
//	public HXResponseObject friendDrop(String username, String friend) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/contacts/users/" + friend, _Global.HTTP_METHOD_DELETE,
//				null, auth, null);
//	}
//
//	public HXResponseObject friendList(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/contacts/users", _Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_ARRAY);
//	}
//
//	public HXResponseObject blockSave(String username, String[] friend)
//			throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("usernames", friend);
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/blocks/users", _Global.HTTP_METHOD_POST, param, auth,
//				HXResponseObject.DATA_ARRAY);
//	}
//
//	public HXResponseObject blockDrop(String username, String friend) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/blocks/users/" + friend, _Global.HTTP_METHOD_DELETE, null,
//				auth, HXResponseObject.DATA_ARRAY);
//	}
//
//	public HXResponseObject blockList(String username) throws Exception {
//		return service.request(_Global.URL_USER + "/" + username
//				+ "/blocks/users", _Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_ARRAY);
//	}
//
//	public HXResponseObject groupSave(String owner, String name, String desc,
//			Integer limit, String[] friend) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("groupname", name);
//		param.put("desc", desc);
//		param.put("public", true);
//		param.put("maxusers", limit);
//		param.put("approval", true);
//		param.put("owner", owner);
//		param.put("members", friend);
//		return service.request(_Global.URL_GROUP, _Global.HTTP_METHOD_POST,
//				param, auth, HXResponseObject.DATA_GROUP);
//	}
//
//	public HXResponseObject groupDrop(String id) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id,
//				_Global.HTTP_METHOD_DELETE, null, auth, HXResponseObject.DATA_GROUP);
//	}
//
//	public HXResponseObject groupModify(String id, String name, String desc,
//			Integer limit) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("groupname", name);
//		param.put("description", desc);
//		param.put("maxusers", limit);
//		return service.request(_Global.URL_GROUP + "/" + id,
//				_Global.HTTP_METHOD_PUT, param, auth,
//				HXResponseObject.DATA_GROUP_UPDATE);
//	}
//
//	public HXResponseObject groupModifyOwner(String id, String owner) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("newowner", owner);
//		return service
//				.request(_Global.URL_GROUP + "/" + id, _Global.HTTP_METHOD_PUT,
//						param, auth, HXResponseObject.DATA_GROUP_OWNER);
//	}
//
//	public HXResponseObject groupGet(String[] id) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + this.param(id),
//				_Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_GROUP_LIST_NEW);
//	}
//
//	public HXResponseObject groupList(Long size, String cursor, Long start)
//			throws Exception {
//		return service.request(
//				_Global.URL_GROUP + "?" + this.page(size, cursor, start),
//				_Global.HTTP_METHOD_GET, null, auth, HXResponseObject.DATA_GROUP_LIST);
//	}
//
//	public HXResponseObject groupFriendSave(String id, String friend) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/users/"
//				+ friend, _Global.HTTP_METHOD_POST, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND);
//	}
//
//	public HXResponseObject groupFriendSave(String id, String[] friend)
//			throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("usernames", friend);
//		return service.request(_Global.URL_GROUP + "/" + id + "/users",
//				_Global.HTTP_METHOD_POST, param, auth,
//				HXResponseObject.DATA_GROUP_FRIEND);
//	}
//
//	public HXResponseObject groupFriendDrop(String id, String friend) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/users/"
//				+ friend, _Global.HTTP_METHOD_DELETE, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND);
//	}
//
//	public HXResponseObject groupFriendDrop(String id, String[] friend)
//			throws Exception {
//		return service.request(
//				_Global.URL_GROUP + "/" + id + "/users/" + this.param(friend),
//				_Global.HTTP_METHOD_DELETE, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND_LIST);
//	}
//
//	public HXResponseObject groupFriendList(String id) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/users",
//				_Global.HTTP_METHOD_GET, null, auth,
//				HXResponseObject.DATA_GROUP_LIST_MEMBER);
//	}
//
//	public HXResponseObject groupBlackSave(String id, String friend) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/blocks/users/"
//				+ friend, _Global.HTTP_METHOD_POST, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND);
//	}
//
//	public HXResponseObject groupBlackSave(String id, String[] friend) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("usernames", friend);
//		return service.request(_Global.URL_GROUP + "/" + id + "/blocks/users",
//				_Global.HTTP_METHOD_POST, param, auth,
//				HXResponseObject.DATA_GROUP_FRIEND_LIST);
//	}
//
//	public HXResponseObject groupBlackDrop(String id, String friend) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/blocks/users/"
//				+ friend, _Global.HTTP_METHOD_DELETE, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND);
//	}
//
//	public HXResponseObject groupBlackDrop(String id, String[] friend) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/blocks/users/"
//				+ this.param(friend), _Global.HTTP_METHOD_DELETE, null, auth,
//				HXResponseObject.DATA_GROUP_FRIEND_LIST);
//	}
//
//	public HXResponseObject groupBlackList(String id) throws Exception {
//		return service.request(_Global.URL_GROUP + "/" + id + "/blocks/users",
//				_Global.HTTP_METHOD_GET, null, auth, HXResponseObject.DATA_ARRAY);
//	}
//
//	public HXResponseObject fileUpload(File file) throws Exception {
//		return service.upload(_Global.URL_FILE, file, auth,
//				HXResponseObject.DATA_ENTITIES);
//	}
//
//	public void fileDown(String id, String share, Boolean thumb, File file)
//			throws Exception {
//		Map<String, String> header = new HashMap<String, String>();
//		header.put("Accept", "application/octet-stream");
//		if (share != null) {
//			header.put("share-secret", share);
//		}
//		if (thumb != null && thumb) {
//			header.put("thumbnail", String.valueOf(thumb));
//		}
//		service.downLoad(_Global.URL_FILE + "/" + id, file, auth, header);
//	}
//
//	public HXResponseObject messageSave(TalkMsg data) throws Exception {
//		String type = data.getMessageType();
//		if (data.getTargetType() == null || type == null
//				|| data.getFrom() == null || data.getTarget() == null) {
//			return null;
//		}
//		Map<String, Object> msg = new HashMap<String, Object>();
//		switch (data.getType()) {
//		case 1:
//			msg.put("type", type);
//			msg.put("msg", data.getMsg());
//			break;
//		case 2:
//			msg.put("type", type);
//			msg.put("url", _Global.URL_FILE + "/" + data.getFile_id());
//			msg.put("filename", data.getFilename());
//			msg.put("secret", data.getSecret());
//			Map<String, Integer> temp = new HashMap<String, Integer>();
//			temp.put("width", data.getWidth());
//			temp.put("height", data.getHeight());
//			msg.put("size", temp);
//			break;
//		case 3:
//			msg.put("type", type);
//			msg.put("url", _Global.URL_FILE + "/" + data.getFile_id());
//			msg.put("filename", data.getFilename());
//			msg.put("secret", data.getSecret());
//			msg.put("length", data.getLength());
//			break;
//		case 4:
//			msg.put("type", type);
//			msg.put("filename", data.getFilename());
//			msg.put("thumb", _Global.URL_FILE + "/" + data.getThumb());
//			msg.put("length", data.getLength());
//			msg.put("secret", data.getSecret());
//			msg.put("file_length", data.getFile_length());
//			msg.put("thumb_secret", data.getThumb_secret());
//			msg.put("url", _Global.URL_FILE + "/" + data.getFile_id());
//			break;
//		case 5:
//			msg.put("type", type);
//			msg.put("action", data.getAction());
//			break;
//		}
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("target_type", data.getTargetType());
//		param.put("from", data.getFrom());
//		param.put("target", data.getTarget());
//		param.put("msg", msg);
//		param.put("ext", data.getExt());
//		return service.request(_Global.URL_MESSAGES, _Global.HTTP_METHOD_POST,
//				param, auth, HXResponseObject.DATA_MAP);
//	}
//
//	public HXResponseObject messageList(Long size, String cursor, Long start)
//			throws Exception {
//		return service.request(
//				_Global.URL_MESSAGES + "?" + this.page(size, cursor, start),
//				_Global.HTTP_METHOD_GET, null, auth, null);
//	}
//
//	public HXResponseObject chatList(Long size, String cursor, Long start)
//			throws Exception {
//		return service.request(
//				_Global.URL_CHAT + "?" + this.page(size, cursor, start),
//				_Global.HTTP_METHOD_GET, null, auth, HXResponseObject.DATA_CHAT_LIST);
//	}
//
//	public HXResponseObject roomSave(String owner, String name, String desc,
//			Integer limit, String[] friend) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("name", name);
//		param.put("description", desc);
//		param.put("maxusers", limit);
//		param.put("owner", owner);
//		param.put("members", friend);
//		return service.request(_Global.URL_ROOM, _Global.HTTP_METHOD_POST,
//				param, auth, HXResponseObject.DATA_ROOM);
//	}
//
//	public HXResponseObject roomModify(String id, String name, String desc,
//			Integer limit) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("name", name);
//		param.put("description", desc);
//		param.put("maxusers", limit);
//		return service.request(_Global.URL_ROOM + "/" + id,
//				_Global.HTTP_METHOD_PUT, param, auth, HXResponseObject.DATA_ROOM_REDO);
//	}
//
//	public HXResponseObject roomDrop(String id) throws Exception {
//		return service
//				.request(_Global.URL_ROOM + "/" + id,
//						_Global.HTTP_METHOD_DELETE, null, auth,
//						HXResponseObject.DATA_ROOM_REDO);
//	}
//
//	public HXResponseObject roomGet(String id) throws Exception {
//		return service.request(_Global.URL_ROOM + "/" + id,
//				_Global.HTTP_METHOD_GET, null, auth, HXResponseObject.DATA_ROOM_LIST);
//	}
//
//	public HXResponseObject roomList() throws Exception {
//		return service.request(_Global.URL_ROOM, _Global.HTTP_METHOD_GET, null,
//				auth, HXResponseObject.DATA_ROOM_LIST);
//	}
//
//	public HXResponseObject roomFriendSave(String id, String friend) throws Exception {
//		return service.request(
//				_Global.URL_ROOM + "/" + id + "/users/" + friend,
//				_Global.HTTP_METHOD_POST, null, auth, HXResponseObject.DATA_ROOM_REDO);
//	}
//
//	public HXResponseObject roomFriendSave(String id, String[] friend) throws Exception {
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("usernames", friend);
//		return service.request(_Global.URL_ROOM + "/" + id + "/users",
//				_Global.HTTP_METHOD_POST, param, auth, HXResponseObject.DATA_ROOM_REDO);
//	}
//
//	public HXResponseObject roomFriendDrop(String id, String friend) throws Exception {
//		return service
//				.request(_Global.URL_ROOM + "/" + id + "/users/" + friend,
//						_Global.HTTP_METHOD_DELETE, null, auth,
//						HXResponseObject.DATA_ROOM_REDO);
//	}
//
//	public HXResponseObject roomFriendDrop(String id, String[] friend) throws Exception {
//		return service.request(
//				_Global.URL_ROOM + "/" + id + "/users/" + this.param(friend),
//				_Global.HTTP_METHOD_DELETE, null, auth,
//				HXResponseObject.DATA_ROOM_REDO_LIST);
//	}
//
//	private String param(String[] data) {
//		StringBuffer param = new StringBuffer();
//		for (String temp : data) {
//			param.append(temp + ",");
//		}
//		return param.deleteCharAt(param.length() - 1).toString();
//	}
//
//	private String page(Long size, String cursor, Long start) throws Exception {
//		StringBuffer res = new StringBuffer("limit="
//				+ (size != null ? size : _Global.APP_PAGE_SIZE));
//		if (cursor != null) {
//			res.append("&cursor=" + cursor);
//		}
//		if (start != null) {
//			Long now = System.currentTimeMillis();
//			res.append("&ql=select * where timestamp>" + (now - start)
//					+ " and timestamp<" + now);
//		}
//		return URLEncoder.encode(res.toString(), "UTF-8");
//	}
//}